package com.igroupes.ovineherd.model.attribute.file;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.CheckboxStyle;
import com.igroupes.ovineherd.model.Style.Group;
import com.igroupes.ovineherd.model.Style.ImageStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 *图片
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
@Component
public class ImageModelData extends FileBasicAttribute {


    @Value("${ovhapi.file.url}")
    private String fileUrl;

    private static String url;
    @PostConstruct
    public void  into(){
        url = fileUrl;
    }
    public static final String RATIO_LABEL = "图片比例";
    public static final String CHECK_SUFFIX_LABEL = "允许照片格式";
//    private CheckboxStyle ratio;

    public ImageModelData() {
        super();
//        this.ratio = new CheckboxStyle(RATIO_LABEL, "不限" , getRatio());
        this.checkSuffix = new CheckboxStyle(CHECK_SUFFIX_LABEL,"", getCheckbox());
    }
    private List<Group> getRatio() {
        List<Group> groups = new ArrayList<>();
        groups.add(new Group("4:3", "4:3"));
        groups.add(new Group("16:9", "16:9"));
        groups.add(new Group("1:1", "1:1"));
        groups.add(new Group("不限", "不限", true));
        return groups;
    }
    private List<Group> getCheckbox() {
        List<Group> groups = new ArrayList<>();
        groups.add(new Group("jpeg",".jpeg"));
        groups.add(new Group("png",".png"));
        groups.add(new Group("gif",".gif"));
        groups.add(new Group("svg",".svg"));
        return groups;
    }
    public Boolean verifyField(ImageModelData modelData) {
        super.verifyField(modelData);
//        new CheckboxStyle().verify(modelData.getRatio());
        return true;
    }

    public Boolean verifyFieldV2(ImageModelData basic) {
        super.verifyFieldV2(basic);
//        new CheckboxStyle().verify(new CheckboxStyle(CHECK_SUFFIX_LABEL,basic.getRatio(), getCheckbox()));
        return true;
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        ImageModelData modelData = (ImageModelData) parentModelData.getAttributes();
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
//        jsonObject.set("ratio", CheckUtils.fields(modelData.getRatio()));
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
//        jsonObject.set("ratio", new CheckboxStyle(RATIO_LABEL, modelDataJsonAttributeQuery.getJson().get("ratio") , getRatio()));
        jsonObject.set("checkSuffix", new CheckboxStyle(CHECK_SUFFIX_LABEL,modelDataJsonAttributeQuery.getJson().get("checkSuffix"), getCheckbox()));
        return jsonObject;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses = super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity ) {

        StringBuilder checkSuffixList = new StringBuilder(Convert.toStr(dataModelAttributeEntity.getJson().get("checkSuffix")));
        if(ObjectUtil.isNotNull(dataModelAttributeEntity.getJson().get("suffix"))){
            List<String> suffix = ListUtils.parseString(dataModelAttributeEntity.getJson().get("suffix").toString());
            for (String str :
                    suffix) {
                checkSuffixList.append(",").append(str);
            }
        }
        return new JSONObject( new ImageStyle(
                dataModelAttributeEntity.getName(),
                "",
                Convert.toInt(dataModelAttributeEntity.getJson().get("size")),
                checkSuffixList.toString(),
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()), url));
    }
}
